/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.cx1.dao.impl;

import com.ibatis.sqlmap.client.SqlMapClient;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import org.cx1.dao.api.IAmenityDao;
import org.cx1.model.Amenity;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author tina
 */
public class AmenityDao implements IAmenityDao {
    
    private SqlMapClient sqlMapClient;

    public AmenityDao() {
        sqlMapClient = IbatisConnectionFactory.getSqlMapClient();
    }
 
    @Override
    public String add(Amenity amenity) {   
        String amenityId = "";
        
        try {
            sqlMapClient.insert("amenity.add", amenity);
            amenityId = amenity.getId();
            
        } catch (SQLException ex) {
            Logger.getLogger(AmenityDao.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return amenityId;
    }

    @Override
    public int update(Amenity amenity) {
        int noOfRowsAffected = 0;
        
        try {
            noOfRowsAffected = sqlMapClient.update("amenity.update", amenity);
        } catch (SQLException ex) {
            Logger.getLogger(AmenityDao.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return noOfRowsAffected;
    }

    @Override
    public Amenity getById(String id) {
        Amenity amenity = null;
        
        try {
            amenity = (Amenity) sqlMapClient.queryForObject("amenity.getById", id);
        } catch (SQLException ex) {
            Logger.getLogger(AmenityDao.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return amenity;
    }

    @Override
    public List<Amenity> getAll() {
        List<Amenity> amenities = new ArrayList<Amenity>();
        
        try {
            amenities = sqlMapClient.queryForList("amenity.getAll");
        } catch (SQLException ex) {
            Logger.getLogger(AmenityDao.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return amenities;
    }

    @Override
    public int deleteById(String id) {
        int noOfRowsAffected = 0;
        
        try {
            noOfRowsAffected = sqlMapClient.delete("amenity.deleteById", id);
        } catch (SQLException ex) {
            Logger.getLogger(AmenityDao.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return noOfRowsAffected;
    }
}
